home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
12984
/
12984.xpi
/
chrome
/
VideoDownloaderToolbar.jar
/
content
/
docanalyzer.js
< prev
next >
Wrap
Text File
|
2010-01-29
|
5KB
|
191 lines
if(!com) var com={};
if(!com.VidBar) com.VidBar={};
com.VidBar.DocUtil = {
xpGetSingleNode : function(node, xpath) {
var anode = node.ownerDocument.evaluate(xpath, node, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
return anode;
},
xpGetNodes : function(node, xpath) {
var nodes = [];
var xpr = node.ownerDocument.evaluate(xpath, node, null,
XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var node0 = xpr.iterateNext();
while (node0 != null) {
nodes.push(node0);
node0 = xpr.iterateNext();
}
return nodes;
},
xpGetStrings : function(node, xpath) {
var strings = [];
var xpr = node.ownerDocument.evaluate(xpath, node, null,
XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var node0 = xpr.iterateNext();
while (node0 != null) {
if (node0.nodeType == Node.TEXT_NODE)
strings.push(node0.nodeValue);
else if (node0.firstChild != null
&& node0.firstChild.nodeType == Node.TEXT_NODE)
strings.push(node0.firstChild.nodeValue);
node0 = xpr.iterateNext();
}
return strings;
},
xpGetString : function(node, xpath) {
var text = node.ownerDocument.evaluate(xpath, node, null,
XPathResult.STRING_TYPE, null).stringValue;
return text;
}
}
com.VidBar.LinkAnalyzer = function(pref) {
this.pref = pref;
}
com.VidBar.LinkAnalyzer.prototype = {
name : "LinkAnalyzer",
analyzeDoc : function(doc) {
// com.VidBar.__d("com.VidBar.LinkAnalyzer.analyzeDoc");
var list = [];
try {
if (doc == null)
doc = content.document;
var mediaPattern = this.pref.getMediaExtensions();
var pat = new RegExp("^.*\\.(?:" + mediaPattern + ")$", "i");
var baseUrl = Components.classes["@mozilla.org/network/standard-url;1"]
.createInstance(Components.interfaces.nsIURI);
baseUrl.spec = doc.URL;
var dom = doc.documentElement;
var allHRefs = {};
var linkNodes = com.VidBar.DocUtil.xpGetNodes(dom, ".//a[@href]", {});
for (var i = 0; i < linkNodes.length; i++) {
try {
var node = linkNodes[i];
var href = node.getAttribute("href");
if (allHRefs[href] != null)
continue;
allHRefs[href] = "";
if (pat.exec(href) == null)
continue;
var hrefParts = /^(.*[^0-9])?([0-9]+)([^\/]*?\.[^\/]*?)$/
.exec(href);
var extension = /.*\.(.*?)$/.exec(hrefParts[3])[1];
var mediaUrl = baseUrl.resolve(href);
var filename = /.*\/(.*?)$/.exec(mediaUrl)[1];
var label = filename;
try {
var ih = node.innerHTML;
label = ih.split(/<\/?[^>]+>/).join("");
} catch (e) {
}
if (label == "")
label = filename;
var data = {
type : "link",
label : label,
filename : filename,
extension : extension,
pageUrl : doc.URL,
mediaUrl : mediaUrl,
referrer : doc.URL
};
list.push(data);
} catch (e) {
com.VidBar.__e("com.VidBar.LinkAnalyzer.analyzeDoc: " + e);
}
}
} catch (e) {
com.VidBar.__e("com.VidBar.LinkAnalyzer.analyzeDoc: " + e);
}
return list;
}
}
com.VidBar.YouTubeAnalyzer = function(pref) {
this.pref = pref;
}
com.VidBar.YouTubeAnalyzer.prototype = {
name : "YoutubeAnalyzer",
analyzeDoc : function(doc) {
// com.VidBar.__d("com.VidBar.YouTubeAnalyzer.analyzeDoc");
var list = [];
try {
if (/^http:\/\/[^\/]*\.?youtube\.[^\/\.]+/.test(doc.URL)) {
var dom = doc.documentElement;
var scripts = com.VidBar.DocUtil.xpGetStrings(dom, ".//script/text()", {});
var videoId = null;
var t = null;
for (var i = 0; i < scripts.length; i++) {
var script = scripts[i];
var match = /video_id=([^&]+)(?:&.*)&t=([^&]+)/m
.exec(script);
if (match != null && match.length == 3) {
videoId = match[1];
t = match[2];
break;
}
var match = /[&\?]t=(.*?)&.*video_id=(.*?)(?:&|")/m
.exec(script);
if (match != null && match.length == 3) {
videoId = match[2];
t = match[1];
break;
}
}
if (videoId == null || t == null) {
var embeds = com.VidBar.DocUtil.xpGetStrings(dom, ".//embed/@src", {});
for (var i = 0; i < embeds.length; i++) {
var embed = embeds[i];
var match = /video_id=(.*?)&.*t=(.*?)(?:&|")/m
.exec(embed);
if (match != null && match.length == 3) {
videoId = match[1];
t = match[2];
break;
}
}
if (videoId == null || t == null) {
return;
}
}
var title = com.VidBar.DocUtil.xpGetString(dom,
"/html/head/meta[@name='title']/@content");
var url = "http://www.youtube.com/get_video?video_id="
+ videoId + "&t=" + t;
var fileName = title;
fileName = fileName.replace(/[^a-zA-Z0-9\.\-]/g, "_");
var data = {
type : "youtube",
label : title,
filename : fileName + ".flv",
extension : "flv",
pageUrl : doc.URL,
mediaUrl : url,
referrer : doc.URL
};
list.push(data);
}
} catch (e) {
com.VidBar.__e("com.VidBar.YouTubeAnalyzer.analyzeDoc error: " + e);
}
return list;
}
};